package com.ck.server.controller.test;

import com.ck.common.bean.ReturnResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author ck
 * @date 2023/3/17 14:31
 */
@RestController
@RequestMapping("okHttp")
@Slf4j
@Api(tags = "OkHttp测试接口")
public class OkHttpController {

    /**
     * 通过单元测试，自己调用自己
     */


    /**
     * OkHttp Get请求
     * @param request
     * @param name
     * @param age
     * @return
     */
    @GetMapping("testGet")
    @ApiOperation(value = "OkHttp Get请求")
    public ReturnResult testGet(HttpServletRequest request,
                                @RequestParam("userName") String name,
                                @RequestParam("age")      String age ){
        log.info("收到OkHttp请求参数,"+"name: {}\t"+"age: {}",name,age);


        /**
         * 后续可以添加一个拦截器用来认证请求头中的内容是否有权限
         * 执行后续操作
         */
        String key = request.getHeader("privateKey");
        log.info("接收到请求头中的内容:{}",key);
        return ReturnResult.success(null);
    }


    /**
     * OkHttp Post请求
     */
    @PostMapping("testPost")
    @ApiOperation(value = "OkHttp Post请求")
    public ReturnResult testPost(HttpServletRequest request,
                                 @RequestBody Map<String,String> map){
        log.info("收到OkHttp请求参数: {}",map);
        String key = request.getHeader("privateKey");
        log.info("接收到请求头中的内容:{}",key);
        return ReturnResult.success(null);
    }
}
